package com.meiyu.live.util;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;

import com.meiyu.live.R;


/**
 * Created by ICE on 2015/11/16.
 */
public class RippleUtils {

    public static int DefaultNightRippleColor = Color.parseColor("#36ffffff");
    public static int DefaultDayRippleColor = Color.parseColor("#36FB3D4F");


    /**
     * 使用指定的颜色进行水波纹设置
     *  用于checkbox
     * @param view
     * @param color
     */
    public static void setRippleCheckDrawable(View view, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Drawable mask = view.getContext().getResources().getDrawable(R.drawable.checkbox_ripple_mask);
            setRippleDrawable(view, mask, color);
        }
    }


    /**
     * 使用指定的颜色进行水波纹设置
     *
     * @param view
     * @param color
     */
    public static void setRippleDrawable(View view, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ColorDrawable mask = new ColorDrawable();
            mask.setColor(view.getContext().getResources().getColor(android.R.color.white));
            setRippleDrawable(view, mask, color);
        }
    }


    /**
     * 使用指定的颜色进行水波纹设置
     * 使用指定的mask进行mask设置
     *
     * @param view
     * @param color
     */
    public static void setRippleDrawable(View view, Drawable mask, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Drawable content = view.getBackground();
            ColorStateList rippleColor = ColorStateList.valueOf(color);
            RippleDrawable drawable = new RippleDrawable(rippleColor, content, mask);
            view.setBackground(drawable);
        }
    }


    /**
     * 给指定View 设定波纹效果
     * 如果View设置了ContentDescription 则直接使用ContentDescription设置的颜色进行设定波纹效果
     * 如果没有设置ContentDescription 或者设置错误 则直接使用默认的颜色进行设置
     *
     * @param view
     */
    public static void setRippleDrawable(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CharSequence contentDescription = view.getContentDescription();
            int ripcolor = 0;
            if (!TextUtils.isEmpty(contentDescription)) {
                try {
                    ripcolor = Color.parseColor(contentDescription.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (ripcolor == 0) {
//                if (ConfigurationManager.isDarkModeSwitchOpened(view.getContext())) {
//                    ripcolor = DefaultNightRippleColor;
//                } else {
                    ripcolor = DefaultDayRippleColor;
//                }
            }
            setRippleDrawable(view, ripcolor);
        }
    }


    /**
     * 使用指定的颜色进行水波纹设置
     * 用于checkBox
     * @param view
     */
    public static void setRippleCheckDrawable(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CharSequence contentDescription = view.getContentDescription();
            int ripcolor = 0;
            if (!TextUtils.isEmpty(contentDescription)) {
                try {
                    ripcolor = Color.parseColor(contentDescription.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (ripcolor == 0) {
//                if (ConfigurationManager.isDarkModeSwitchOpened(view.getContext())) {
//                    ripcolor = DefaultNightRippleColor;
//                } else {
                    ripcolor = DefaultDayRippleColor;
//                }
            }
            setRippleCheckDrawable(view, ripcolor);
        }
    }


}
